/**
 * 
 */
package es.aradesh.ministryassist.logic.ministry;

import java.util.Iterator;
import java.util.List;

import android.content.ContentValues;

import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.db.TerritoryColumns;

/**
 * @author xmorales
 *
 */
public class Territory implements MinistryElement{
	private long id;
	private int number;
	private String town;

	/**
	 * @param id
	 * @param number
	 * @param town
	 */
	public Territory(long id, int number, String town) {
		this.id = id;
		this.number = number;
		this.town = town;
	}
	
	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @return the number
	 */
	public int getNumber() {
		return number;
	}
	/**
	 * @return the town
	 */
	public String getTown() {
		return town;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @param number the number to set
	 */
	public void setNumber(int number) {
		this.number = number;
	}
	/**
	 * @param town the town to set
	 */
	public void setTown(String town) {
		this.town = town;
	}
	
	public boolean delete(DbAdapter mDb){
		List<Street> ls = StreetBuilder.getStreetsByTerritory(mDb, this);
		Iterator<Street> lsi = ls.iterator();
		while(lsi.hasNext()){
			lsi.next().delete(mDb);
		}
		return mDb.removeById(DbAdapter.TABLE_TERRITORY, this.id);
	}
	
	public void refreshDB(DbAdapter mDb){
		ContentValues cv = new ContentValues(2);
		cv.put(TerritoryColumns.NUMBER, this.number);
		cv.put(TerritoryColumns.TOWN, this.town);
		mDb.updateById(DbAdapter.TABLE_TERRITORY, this.id, cv);	
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String append = "";
		if (this.number != 0) append = " #"+this.number;
		return this.town + append;
	}

	public int getActivityEditType() {
		// TODO Auto-generated method stub
		return MinistryElement.ACTIVITY_EDIT_TERRITORY;
	}

	/* (non-Javadoc)
	 * @see es.aradesh.ministryassist.logic.ministry.MinistryElement#getParam(java.lang.String)
	 */
	public Object getParam(String parameter) {
		Object result = null;
		if(parameter == TerritoryColumns.NUMBER) result = new Integer(this.number);
		else if(parameter == TerritoryColumns.TOWN) result = this.town;
		return result;
	}
	
}